<?php
/*
* Author：鑫宇工作室
* QQ：639519801
* url：www.xinyuream.com
*/
function api_lock(){
	$app=check_appid();
	$username=input('username');
	$supassword=pwdMd5(input('supassword'));
	$sql="select * from xy_user where username='{$username}'";
	$re=query($sql);
	if(strtotime($re[0]['locktime'])>time()-(config('locktime')*86400)){
		apijson(201,"距离上次解锁小于".config('locktime')."天");
	}
	$sql="update xy_user set vcode='',locktime=now() where username='{$username}'";
	query($sql,2);
	apijson(201,"解绑成功");
}

function api_check(){
	$app=check_appid();
    $token=input('token');
    $vcode=input('vcode');
    $re = \model\UserModel::getInstance(1)->find(['vcode'=>$vcode,'appid'=>$app['id']]);
	if(!$re){
		apijson(201,"客户端不存在");
	}
	if(time()>strtotime($re['viptime'])){
		apijson(201,"账号已过期");
	}
	if($re['status'] !=1){
		apijson(201,"账号已禁用");
	}
	if(config('dklogin')!=1){
		if($token != $re['token']){
			apijson(201,"登录失效");
		}
	}
    \model\UserModel::getInstance()->update(['id'=>$re['id']],['onlinetime'=>date('Y-m-d H:i:s')]);
	apijson(200,$re['viptime']);
}

function api_login(){
	$app=check_appid();

	$vcode=input('vcode');
	$ver=input('ver');
    if(strlen($vcode)!=32){
        apijson(202,"数据异常,请联系作者");
    }

    $re = \model\UserModel::getInstance(1)->find(['vcode'=>$vcode,'appid'=>$app['id']]);
	if(!$re){
        // 调用注册接口
        api_reg($vcode,$ver);exit;
	}
	if(time()>strtotime($re['viptime'])){
//		apijson(201,"账号已过期"); // 过期不判断  账号依然要能登陆
	}
	if($re['status'] !=1){
		apijson(201,"账号已禁用");
	}
    $token = getLoginToken($re['id'],$ver);
    apijson(200,$token);
}

function getLoginToken($uid,$ver){
    $time = date('Y-m-d H:i:s');
    $token=pwdMd5(rand(1111,9999).uniqid().$time);
    $ip=get_ip();
    $res = \model\UserModel::getInstance()->update(['id'=>$uid],[
        'token'=>$token,
        'logintime'=>$time,
        'loginip'=>$ip,
        'version'=>$ver,
    ]);
    return $token;
}
function addGetApi($appid){
    $ip = get_ip();
    $arr = \ip\Ip::find($ip);
    $data = [
        'ip'=>$ip,
        'ip_text'=>join('',$arr),
        'create_time'=>date('Y-m-d H:i:s'),
        'app_id'=>$appid,
        'url'=>$_SERVER['REQUEST_URI']

    ];
    \model\GetModel::getInstance(1)->create($data);
}

function api_pay(){
	$app=check_appid();
    $vcode=input('vcode');
	$card=input('card');
    $user = \model\UserModel::getInstance(1)->find(['vcode'=>$vcode,'appid'=>$app['id']]);
	if(!$user){
		apijson(201,"客户端不存在");
	}
    $rs = \model\CardModel::getInstance(1)->find(['status'=>1,'cardnum'=>$card]);
	if(!$rs){
		apijson(201,"卡密不正确");
	}
	$num=get_card_type($rs['stype'],true);
	$time=strtotime($user['viptime'])>=time()?strtotime($user['viptime']):time();
	$viptime=date('Y-m-d H:i:s',$time+$num*86400);

    // 给用户加时间
    \model\UserModel::getInstance(1)->update(['id'=>$user['id']],['viptime'=>$viptime]);
    \model\CardModel::getInstance(1)->update(['id'=>$rs['id']],[
        'status'=>2,
        'stime'=>date('Y-m-d H:i:s'),
        'username'=>$user['id'],
    ]);
	insertlog($user['username'],"卡密充值:".$card);
	apijson(200,"充值成功,到期时间：".$viptime);
}


function api_repass(){
	$app=check_appid();
	$username=input('username');
	$supassword=input('supassword');
	$password=input('password');
	$supass=pwdMd5($supassword);
	$pass=pwdMd5($password);
	if(strlen($supassword)<config('password')){
		apijson(201,"账号或超级密码错误");
	}
	if(strlen($password)<config('password')){
		apijson(201,"新密码最少6位");
	}
	$re=query("select * from xy_user where username='{$username}'");
	if(!$re){
		apijson(201,"账号或超级密码错误");
	}
	if($re[0]['supassword'] !=$supass){
		apijson(201,"账号或超级密码错误");
	}
	$sql="update xy_user set password='{$pass}' where username='{$username}'";
	query($sql,2);
	insertlog($username,"找回密码");
	apijson(200,"密码修改成功");
}

function api_pass(){
	$app=check_appid();
	$username=input('username');
	$oldpassword=input('oldpassword');
	$password=input('password');
	$opass=pwdMd5($oldpassword);
	$pass=pwdMd5($password);
	if(strlen($oldpassword)<config('password')){
		apijson(201,"账号或原始密码错误");
	}
	if(strlen($password)<config('password')){
		apijson(201,"新密码最少6位");
	}
	$re=query("select * from xy_user where username='{$username}'");
	if(!$re){
		apijson(201,"账号或原始密码错误");
	}
	if($re[0]['password'] !=$opass){
		apijson(201,"账号或原始密码错误");
	}
	$sql="update xy_user set password='{$pass}' where username='{$username}'";
	query($sql,2);
	insertlog($username,"修改密码");
	apijson(200,"密码修改成功");
}

function api_reg($vcode,$ver){
	$app=check_appid();
	if(config('regopen')!=1){
		apijson(202,"当前关闭注册");
	}
    $re = \model\UserModel::getInstance(1)->find(['vcode'=>$vcode,'appid'=>$app['id']]);
	if($re){
		apijson(202,"设备已存在");
	}
    // 新用户 赠送
    $viptime=date('Y-m-d H:i:s',time()+config('regzs')*60);
    $ip = get_ip();
    $ipText = \ip\Ip::find($ip);
    if($ipText)$ipText = isset($ipText[1])?$ipText[1]:'';
    $username = $ipText.'-'.getRandomNickname();
    $uid = \model\UserModel::getInstance()->create([
        'vcode'=>$vcode,
        'regtime'=>date('Y-m-d H:i:s'),
        'appid'=>$app['id'],
        'loginip'=>$ip,
        'status'=>1,
        'viptime'=>$viptime,
        'username'=>$username,
    ]);
	insertlog($username,"注册账号");
	insertlog($username,"注册账号赠送：".config('regzs')."分钟");
    $token = getLoginToken($uid,$ver);
	apijson(200,$token);
}

function api_init(){
	$app=check_appid();
	$sql="select content from xy_news where appid='{$app['id']}' order by id desc limit 1";
	$re=query($sql);
	if($re){
        $content = str_replace("\n",'【换行】',$re[0]['content']);
		apijson(200,$content);
	}
	apijson(200,"暂无公告");
}

/**
 * 检查版本信息
 * @return void
 */
function api_version(){
    $app = check_appid();
    $ver = input('ver');
    if(version_compare($app['version'],$ver)==1){
        // 发现新版本
        // 保存的文件名|版本号|下载地址|公告
        $ex = '*#*##*#*';//分隔符
        // 对文件名中的中文 进行url编码
        $basename = basename($app['newurl']);
        $app['newurl'] = str_replace($basename,urlencode($basename),$app['newurl']);
        $msg = $app['appname'].$ex.$app['version'].$ex.$app['newurl'].$ex.$app['new_notice'];
        apijson($app['force']==1?210:220,$msg);
    }
    apijson(200,$app['version']);
}


function check_appid(){
	global $appid;
	$sql="select * from xy_app where id='{$appid}' and status=1";
	$re=query($sql);
	if($re){
		return $re[0];
	}
	apijson(201,"软件ID不存在");
}

?>